<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('finance.report.loanRecord.createT')">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('finance.report.loanRecord.successTime')">
          <el-date-picker
            v-model="payTime"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('finance.report.loanRecord.paymentChannel')">
          <el-select v-model="searchForm.paymentChannel" clearable :placeholder="$t('common.select')">
            <el-option
              v-for="item in $t('finance.report.loanRecord.paymentChannels')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('finance.report.loanRecord.tradeStatus')">
          <el-select v-model="searchForm.paymentResult" clearable :placeholder="$t('common.select')">
            <el-option
              v-for="item in $t('finance.report.loanRecord.statuss')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="inputData" :placeholder="$t('common.input')" class="input-with-select">
            <el-select slot="prepend" v-model="selectData">
              <el-option
                v-for="(item, index) in $t('finance.report.loanRecord.search')"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-input>
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportList">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card style="margin-top: 10px">
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%;"
      >
        <el-table-column
          prop="createTime"
          :label="tableText['createTime']"
          min-width="170"
        />
        <el-table-column
          prop="paymentChannelStr"
          :label="tableText['paymentChannel']"
          min-width="150"
        />
        <el-table-column
          prop="serialNumber"
          :label="tableText['transactionNumber']"
          min-width="120"
        />
        <el-table-column
          prop="orderNo"
          :label="tableText['orderNo']"
          min-width="170"
        />
        <el-table-column
          prop="payMoney"
          :label="tableText['transactionAmount']"
          min-width="150"
        />
        <el-table-column
          prop="bankName"
          :label="tableText['bankName']"
          min-width="150"
        />
        <el-table-column
          prop="card"
          :label="tableText['bankCard']"
          min-width="150"
        />
        <el-table-column
          prop="userName"
          :label="tableText['cardholder']"
          min-width="100"
        />
        <el-table-column
          prop="tel"
          :label="tableText['phoneNumber']"
          min-width="150"
        />
        <el-table-column
          fixed="right"
          prop="paymentResult"
          :label="tableText['state']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ setValue(scope.row['paymentResult']) }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          prop="payDate"
          :label="tableText['successTime']"
          min-width="170"
        >
          <template slot-scope="scope">
            {{ scope.row['paymentResult'] === 2 ? scope.row['payDate'] : '' }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </el-row>
</template>

<script>
import { getLoanRecord, exportLoanRecord } from '@/api/finance'
import { formatDate } from '@/utils/method'
export default {
  name: 'LoanRecord',
  data() {
    return {
      loading: false,
      tableData: [],
      tableText: {},
      totalNumber: 0,
      time: [new Date(), new Date()],
      payTime: null,
      searchForm: this.params(),
      inputData: null,
      selectData: 'orderNo'
    }
  },
  mounted() {
    this.tableText = this.$t('finance.report.loanRecord')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        orderNo: null,
        serialNumber: null,
        account: null,
        userName: null,
        card: null,
        startTime: null,
        endTime: null,
        payMinDate: null,
        payMaxDate: null,
        paymentResult: '',
        paymentChannel: ''
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.selectData = 'orderNo'
      this.inputData = null
      this.time = [new Date(), new Date()]
      this.payTime = null
      this.getData()
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data.startTime = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data.endTime = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data.startTime = null
        data.endTime = null
      }
      if (this.payTime && this.payTime.length > 1) {
        data.payMinDate = formatDate(new Date(this.payTime[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data.payMaxDate = formatDate(new Date(this.payTime[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data.payMinDate = null
        data.payMaxDate = null
      }
      if (this.selectData && this.inputData) {
        data[this.selectData] = this.inputData
      }
      return data
    },
    getData() {
      this.loading = true
      const data = this.searchData()
      getLoanRecord(data)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    setValue(val) {
      const name = this.$t('finance.report.loanRecord.statuss').filter(item => {
        return item.value === val
      })
      return (name && name[0] && name[0].desc) ? name[0].desc : ''
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    exportList() {
      exportLoanRecord(this.searchData())
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
